/*
 * @description: 
 * @Date: 2022-10-05 13:34:41
 * @LastEditTime: 2022-12-18 23:07:28
 */
import router from "./Router"; //路由表组件
import element, { ElNotification } from "element-plus"; //element
import "element-plus/dist/index.css"; //element Css样式
import zhCn from "element-plus/lib/locale/lang/zh-cn"; //element中文语言包
import 'animate.css';
import moment from "moment/moment";
import * as sm from "./sm2";

/**
 * @description: 动画执行函数 animate.css库
 * @param {*} event dom节点
 * @param {*} AnimateClassName animate的动画类名 无需animate__前缀
 * @param {*} time 动画时间 默认2秒
 * @return {*}
 */
const animate = async (event, AnimateClassName, time = 0.5) => {
    const dom = event.target.classList;
    AnimateClassName = 'animate__' + AnimateClassName;
    dom.add("animate__animated", AnimateClassName);
    setTimeout(_ => {
        dom.remove(AnimateClassName);
    }, time * 1000)
}

const isMobile = () => {
    //获取浏览器navigator对象的userAgent属性（浏览器用于HTTP请求的用户代理头的值）
    var info = navigator.userAgent;
    //通过正则表达式的test方法判断是否包含“Mobile”字符串
    var isPhone = /mobile/i.test(info);
    //如果包含“Mobile”（是手机设备）则返回true
    window.isMobile = isPhone;
    return isPhone;
}


export const downloadFile = (fileName, content) => {
    // 定义触发事件的DOM
    var aLink = document.createElement('a');
    var date = moment().format("YYYY-MM-DD")
    // 创建文件
    var file = new File([content], 'test.txt', { type: 'text/plain' });
    // 定义下载文件名称
    aLink.download = fileName + date + '.json';
    // 根据File对象创建文件 dataURL
    aLink.href = URL.createObjectURL(file);
    // 触发事件下载
    aLink.click();
}


const setTitleColor = (url) => {
    if (isMobile()) {
        let img = document.createElement("img")
        img.src = url;
        img.onload = function () {
            var canvas = document.createElement('canvas')
            canvas.width = img.width;
            canvas.height = img.height;
            canvas.co
            var ctx = canvas.getContext("2d");
            ctx.drawImage(img, 0, 0)
            img.style.display = 'none'
            let imgData = (ctx.getImageData(0, 0, img.width, img.height)).data
            var b = 'rgba(' + imgData[0] + ',' + imgData[1] + ',' + imgData[2] + ',' + imgData[3] + ')'
            document.body.style.backgroundColor = b;
        }
    }
}

export { animate, setTitleColor }
isMobile();

export default {
    v: undefined,
    install(Vue) {
        this.v = Vue;
        Vue.directive("animate", {
            mounted(event, info) {
                animate({ target: event }, info.value)
            },
        })
        this.Use(element, { locale: zhCn });
        this.Use(router);
        this.Set("animate", animate);
    },
    /**
     * @description:Vue挂载组件方法
     * @param {*} obj 组件对象
     * @param {*} other 额外参数
     */
    Use(obj, other = {}) {
        this.v.use(obj, other);
    },
    /**
     * @description:Vue全局方法挂载
     * @param {*} key 方法名
     * @param {*} value 方法函数
     */
    Set(key, value) {
        this.v.config.globalProperties[key] = value;
    },
};


var _hmt = _hmt || [];
(function () {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?8eedde9b6ef879e1375023ae83a10214";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
})();